<?php

namespace App\Admin\Controllers;

use App\Models\Movie;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\User;

class MovieController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Movie);

        $grid->id('ID')->sortable();
        $grid->title('片名');
        $grid->director('导演')->display(function($userId){
            return User::find($userId)->name;
            //return '12312';
        });
        $grid->describe('介绍');
        $grid->rate('价格');
        $grid->released('上映?')->display(function($released){
            return $released?'是':'否';
        });
        $grid->release_at('发行时间');
        //$grid->created_at('创建时间');
        //$grid->updated_at('更新时间');
        $grid->filter(function ($filter) {
            // 设置created_at字段的范围查询
            $filter->disableIdFilter();
            $filter->like('title', '片名');
            $filter->between('created_at', '时间筛选')->datetime();
        });
        //$grid->expandFilter(); //是否展开筛选搜索
        $grid->column('测试列')->display(function(){
            return $this->title.':'.User::find($this->director)->name;
        });
        //$grid->model()->where('id' , '>=', '2');
        //$grid->model()->orderBy('id', 'desc');
        //$grid->disableCreateButton(); //禁止创建按钮
        //$grid->disablePagination(); //禁用分页
        //$grid->disableFilter(); //禁用查询过滤器
        //$grid->disableExport();
        //$grid->disableRowSelector();
        //$grid->disableActions(); //禁用操作按钮
        $grid->actions(function ($actions) {
            //$actions->disableDelete();
            //$actions->disableEdit();
            //$actions->disableView();

            //var_dump($actions->row);
            //var_dump($actions->getKey());
            // append一个操作

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Movie::findOrFail($id));

        $show->id('Id');
        $show->title('Title');
        $show->director('Director');
        $show->describe('Describe');
        $show->rate('Rate');
        $show->released('Released');
        $show->release_at('Release at');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Movie);

        $form->text('title', '电影标题');
        $directors = [
            1 => 'John',
            2 => 'Smith',
            3 => 'Kate' ,
        ];
        $form->select('director', '导演')->options($directors);
        //$form->textarea('describe', '简介');
        $form->UEditor('describe', '简介');
        $form->number('rate', '打分')->default(1);
        $form->switch('released', '发布？');
        $form->datetime('release_at', '发布时间')->default(date('Y-m-d H:i:s'));

        return $form;
    }
}
